package conf

import (
	"net/url"
	"os"
	"path/filepath"
	"strings"

	"github.com/astaxie/beego"
)

var (
	MirrorCacheDir = ""
	MirrorUrl      = ""
	MirrorHost     = ""
	MirrorPath     = ""
	MirrorDir      = ""
	DS             = ""
)

func init() {
	DS = string(os.PathSeparator)
	MirrorUrl = strings.TrimRight(beego.AppConfig.String("mirror-url"), "/")
	mu, err := url.Parse(MirrorUrl)
	if err == nil {
		MirrorHost = mu.Hostname()
	}
	MirrorPath = strings.TrimRight(mu.Path, "/")

	//缓存文件路径
	MirrorDir = MirrorHost
	if len(MirrorPath) > 0 {
		MirrorDir += DS + MirrorPath
	}
	cache, err := filepath.Abs(beego.AppConfig.String("mirror-cache"))
	if err != nil {
		exFile, _ := filepath.Abs(os.Args[0])
		cache = filepath.Dir(exFile) + "/mirrors"
	}
	MirrorCacheDir = cache + DS + MirrorDir

}
